/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.gwc.yy.liuxue.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.gwc.yy.liuxue.entity.Recommend;
import com.gwc.yy.liuxue.entity.School;
import com.gwc.yy.liuxue.entity.YyApplySchool;
import com.gwc.yy.liuxue.service.RecommendService;
import com.gwc.yy.liuxue.service.SchoolService;
import com.gwc.yy.liuxue.service.YyApplySchoolService;

/**
 * 申请记录Controller
 * @author 黎俊涛
 * @version 2017-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/liuxue/yyApplySchool")
public class ApplySchoolController extends BaseController {

	public final static String NO_DATA="no_data";
	public final static String WAITING="waiting";
	@Autowired
	private YyApplySchoolService yyApplySchoolService;
	@Autowired
	private SchoolService schoolService;
	@Autowired
	private RecommendService recommendService;
	
	@ModelAttribute
	public YyApplySchool get(@RequestParam(required=false) String id) {
		YyApplySchool entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = yyApplySchoolService.get(id);
		}
		if (entity == null){
			entity = new YyApplySchool();
		}
		return entity;
	}
	
	@RequiresPermissions("liuxue:yyApplySchool:view")
	@RequestMapping(value = {"list", ""})
	public String list(YyApplySchool yyApplySchool, HttpServletRequest request, HttpServletResponse response, Model model) {
		yyApplySchool.setStudent(UserUtils.getUser());
		Page<YyApplySchool> page = yyApplySchoolService.findPage(new Page<YyApplySchool>(request, response), yyApplySchool); 
		model.addAttribute("page", page);
		return "yy/liuxue/yyApplySchoolList";
	}
	@RequiresPermissions("liuxue:yyApplySchool:view")
	@RequestMapping(value = {"mylist"})
	public String mylist(YyApplySchool yyApplySchool, HttpServletRequest request, HttpServletResponse response, Model model) {
		yyApplySchool.setAdviser(UserUtils.getUser());
		Page<YyApplySchool> page = yyApplySchoolService.findPage(new Page<YyApplySchool>(request, response), yyApplySchool); 
		model.addAttribute("page", page);
		return "yy/liuxue/myApplySchoolList";
	}
	@RequiresPermissions("liuxue:yyApplySchool:view")
	@RequestMapping(value = "form")
	public String form(YyApplySchool yyApplySchool,String recommend, Model model) {
//		if(yyApplySchool.getSchool()!=null&&!StringUtils.isEmpty(yyApplySchool.getSchool().getId()))
//			yyApplySchool.setSchool(schoolService.get(yyApplySchool.getSchool().getId()));
//		if(!StringUtils.isEmpty(recommend)){
//			Recommend recommendEntity=recommendService.get(recommend);
//			yyApplySchool.setSchool(recommendEntity.getSchool());
//			yyApplySchool.setProfession(recommendEntity.getProfession());
//			yyApplySchool.setDepartment(recommendEntity.getSchoolDepartment());
//		}
		model.addAttribute("yyApplySchool", yyApplySchool);
		return "yy/liuxue/yyApplySchoolForm";
	}
	@RequiresPermissions("liuxue:yyApplySchool:view")
	@RequestMapping(value = "apply")
	public String apply(YyApplySchool yyApplySchool,String recommend, Model model) {
		if(yyApplySchool.getSchool()!=null&&!StringUtils.isEmpty(yyApplySchool.getSchool().getId()))
			yyApplySchool.setSchool(schoolService.get(yyApplySchool.getSchool().getId()));
		if(!StringUtils.isEmpty(recommend)){
			Recommend recommendEntity=recommendService.get(recommend);
			yyApplySchool.setSchool(recommendEntity.getSchool());
			yyApplySchool.setProfession(recommendEntity.getProfession());
			yyApplySchool.setDepartment(recommendEntity.getSchoolDepartment());
		}
		model.addAttribute("yyApplySchool", yyApplySchool);
		return "yy/liuxue/yyApplySchool";
	}
	@RequiresPermissions("liuxue:yyApplySchool:edit")
	@RequestMapping(value = "save")
	public String save(YyApplySchool yyApplySchool, Model model, RedirectAttributes redirectAttributes) {
		if(StringUtils.isEmpty(yyApplySchool.getApplyStatus())){
			yyApplySchool.setApplyStatus(NO_DATA);
		}
		if(StringUtils.isEmpty(yyApplySchool.getApplyResult())){
			yyApplySchool.setApplyResult(WAITING);
		}
		if(yyApplySchool.getStudent()==null)
			yyApplySchool.setStudent(UserUtils.getUser());
		if (!beanValidator(model, yyApplySchool)){
			return form(yyApplySchool,null, model);
		}
		
		yyApplySchoolService.save(yyApplySchool);
		addMessage(redirectAttributes, "保存申请成功");
		return "redirect:"+Global.getAdminPath()+"/liuxue/recommend/studentlist";
	}
	
	@RequiresPermissions("liuxue:yyApplySchool:edit")
	@RequestMapping(value = "delete")
	public String delete(YyApplySchool yyApplySchool, RedirectAttributes redirectAttributes) {
		yyApplySchoolService.delete(yyApplySchool);
		addMessage(redirectAttributes, "删除申请成功");
		return "redirect:"+Global.getAdminPath()+"/liuxue/yyApplySchool/?repage";
	}

}